package com.woniuxy.sale.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.model.dto.DemandDetailsDTO;
import com.woniuxy.common.model.entity.DemandGoods;
import com.woniuxy.common.service.FeignPurchaseService;
import com.woniuxy.model.vo.DepotStockManageVo;
import com.woniuxy.model.vo.UpdateSaleOrderVo;
import com.woniuxy.response.CustomPage;
import com.woniuxy.model.entity.ExportTable;
import com.woniuxy.sale.entity.SalePack;
import com.woniuxy.sale.entity.dto.SaleOrderItemDTO;
import com.woniuxy.sale.entity.dto.SalePackDTO;
import com.woniuxy.sale.entity.dto.SalePackManageDTO;
import com.woniuxy.sale.entity.vo.*;
import com.woniuxy.sale.exception.SalePackException;
import com.woniuxy.sale.mapper.SalePackMapper;
import com.woniuxy.sale.service.*;
import com.woniuxy.service.FeignDepotService;
import com.woniuxy.util.SnowflakeIdWorker;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 打包单; 服务实现类
 * </p>
 *
 * @author lio
 * @since 2023-09-07
 */
@Service
@Slf4j
public class SalePackServiceImpl implements SalePackService {

    @Resource
    private SalePackMapper salePackMapper;

    @Resource
    private SaleOrderService saleOrderService;

    @Resource
    private FeignDepotService feignDepotService;

    @Resource
    private ExportTableService exportTableService;

    @Resource
    private SaleOrderItemService saleOrderItemService;

    @Resource
    private FeignPurchaseService feignPurchaseService;

    @GlobalTransactional(rollbackFor = SalePackException.class)
    @Override
    public boolean add(SalePackVo salePackVo) {

        //1.打包商品,扣减库存

        //根据sno查找depot_id和商品信息、库存信息
        SalePackDTO salePackDTO = saleOrderService.findAllBySno(salePackVo.getSno());

        //扣减仓库库存
        for(int i = 0; i < salePackDTO.getOrderItems().size(); i++){

            SaleOrderItemDTO saleOrderItemDTO = salePackDTO.getOrderItems().get(i);
            //商品数量
            Integer count = saleOrderItemDTO.getCount();
            //商品库存
            Integer num = saleOrderItemDTO.getNum();

            synchronized(SalePackServiceImpl.class){
                if(num.compareTo(count) < 0){
                    throw new SalePackException("产品" + saleOrderItemDTO.getGoodsName() + "库存不足!");
                }

                //库存充足,扣库存,调用feign接口
                //传入DepotStockManageVo,返回true或者false
                DepotStockManageVo depotStockManageVo = new DepotStockManageVo();
                depotStockManageVo.setCode(saleOrderItemDTO.getGoodsNumber());
                depotStockManageVo.setDepotId(salePackDTO.getDepotId());
                depotStockManageVo.setNum(saleOrderItemDTO.getCount());
                if(!feignDepotService.delStock(depotStockManageVo).getData()){
                    throw new SalePackException("产品" + saleOrderItemDTO.getGoodsName() + "扣减库存失败!");
                }
            }
        }

        //2.填写打包单
        String pno = "DB" + SnowflakeIdWorker.newId();
        SalePack salePack = new SalePack();
        salePack.setPno(pno).setSno(salePackVo.getSno()).setState("0").setOperator(salePackVo.getOperator());
        if(!salePackMapper.addPack(salePack)){
            throw new SalePackException("打包单新增失败!");
        }

        //3.修改order表的状态
        UpdateSaleOrderVo updateSaleOrderVo = new UpdateSaleOrderVo(salePackVo.getSno(),"4");
        if(!saleOrderService.updateState(updateSaleOrderVo)){
            throw new SalePackException(salePackVo.getSno() + "状态修改失败!");
        }

        return true;
    }

    @Transactional(rollbackFor = SalePackException.class)
    @Override
    public boolean updateState(UpdateSalePackVo updateSalePackVo) {

        if(!salePackMapper.updateState(updateSalePackVo)){
            throw new SalePackException("打包单" + updateSalePackVo.getPno() + "提交失败");
        }
        return true;
    }

    @Override
    public CustomPage<List<SalePackManageDTO>> managePack(SalePackManageVo salePackManageVo) {

        PageHelper.startPage(salePackManageVo.getCurrent(),salePackManageVo.getSize());

        List<SalePackManageDTO> salePackList = salePackMapper.managePack(salePackManageVo);
        PageInfo<SalePackManageDTO> pageInfo = new PageInfo<>(salePackList);

        CustomPage<List<SalePackManageDTO>> customPage = CustomPage.<List<SalePackManageDTO>>builder()
                .currentPage(salePackManageVo.getCurrent())
                .size(salePackManageVo.getSize())
                .totalPage(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .data(salePackList)
                .build();
        return customPage;
    }

    @Override
    public List<String> findAllPno() {
        return salePackMapper.findAllPno();
    }

    @Transactional
    @Override
    public boolean exportSalePack(ExportTable table) {
        return exportTableService.add(table);
    }

    @Transactional(rollbackFor = SalePackException.class)
    @Override
    public boolean addDemand(AddDemandVo addDemandVo) {

        //通过sno查找sid
        Integer sid = saleOrderService.findSidBySno(addDemandVo.getSno());

        //修改orderitem表中的产品状态
        String states = "1";
        for(int i = 0; i < addDemandVo.getGoodsList().size(); i++){
            UpdateItemVo updateItemVo = new UpdateItemVo();
            updateItemVo.setSid(sid);
            updateItemVo.setStates(states);
            updateItemVo.setGoodsNumber(addDemandVo.getGoodsList().get(i).getGoodsNumber());
            if(!saleOrderItemService.updateStatesBySidAndNumber(updateItemVo)){
                throw new SalePackException("产品" + addDemandVo.getGoodsList().get(i).getGoodsNumber() +  "状态修改失败!");
            }
        }

        DemandDetailsDTO demandDetailsDTO = new DemandDetailsDTO();
        demandDetailsDTO.setGoodsList(addDemandVo.getGoodsList());
        feignPurchaseService.insertDemand(demandDetailsDTO);

        return true;
    }
}
